<template>
  <view>
    <!-- 参阅 https://v1.uviewui.com/components/tabbar.html -->
    <u-tabbar
      v-model="current"
      bg-color="#fefefe"
      active-color="#10A28F"
      inactive-color="#999999"
      :list="list"
      :mid-button="true"
      :before-switch="beforeSwitch"
    ></u-tabbar>
  </view>
</template>

<script>
const scanTabbarIndex = 1
const cartTabbarIndex = 3

export default {
  name: 'UFoooter',
  data() {
    return {
      current: 0,
      list: [
        {
          pagePath: '/pages/index/index',
          iconPath: '/static/tabbar/sy.png',
          selectedIconPath: '/static/tabbar/sy-ck.png',
          text: '首页',
          count: 0,
          isDot: true,
          customIcon: false
        },
        // {
        // 	pagePath: "/pages/video-list/video-list",
        // 	iconPath: "/static/tabbar/sp.png",
        // 	selectedIconPath: "/static/tabbar/sp-ck.png",
        // 	text: '视频',
        // 	customIcon: false,
        // },
        {
          pagePath: 'pages/scan/scan',
          iconPath: '/static/tabbar/sys.png',
          selectedIconPath: '/static/tabbar/sys.png',
          text: '扫一扫',
          midButton: true,
          customIcon: false
        },
        // {
        // 	pagePath: "/pages/cart/cart",
        // 	iconPath: "/static/tabbar/gwc.png",
        // 	selectedIconPath: "/static/tabbar/gwc-ck.png",
        // 	text: '购物车',
        // 	count: 0,
        // 	customIcon: false,
        // },
        {
          pagePath: '/pages/ucenter/ucenter',
          iconPath: '/static/tabbar/wd.png',
          selectedIconPath: '/static/tabbar/wd-ck.png',
          text: '我的',
          count: 0,
          isDot: false,
          customIcon: false
        }
      ]
    }
  },
  updated() {
    console.log('updated')
    this.setCartTabBadge()
  },
  methods: {
    beforeSwitch(index) {
      // 不切换扫一扫
      console.debug('beforeSwitch: index=' + index)
      if (index !== scanTabbarIndex) {
        return true
      }
      // #ifdef H5
      setTimeout(() => {
        // H5 中扫码演示
        //const q = 'https://mdycjt.com?type=payment&shopId=1915697490670272514&memberNo=7901693201'
        const q = 'https://mdycjt.com?type=payment&shopId=1943511878332915714&memberNo=8499661101'
        uni.reLaunch({
          url: '/pages/index/splash?q=' + encodeURIComponent(q)
        })
      }, 1500)
      // #endif
      // #ifndef MP-WEIXIN
      this.$u.toast('请在微信小程序中扫码！')
      // #endif
      // #ifdef MP-WEIXIN
      uni.scanCode({
        // scanType: ['QR_CODE'], //条形码
        success: function (res) {
          console.log('扫码结果：' + res.result)
          // 微信小程序
          if (res.errMsg == 'scanCode:ok') {
            // 扫描到的信息
            if (res.result) {
              // console.log('app.scanCode', res.result,encodeURIComponent(res.result))
              // app.isFirst = true
              uni.reLaunch({
                url: '/pages/index/splash?q=' + encodeURIComponent(res.result)
              })
            }
          } else {
            uni.$u.toast('未识别到二维码，请重新尝试！')
          }
        },
        fail: function (res) {
          console.debug(res)
          const msg = res.errMsg.replace('scanCode:fail', '').trim()
          if (msg != 'cancel') {
            uni.$u.toast('扫码出错：' + msg)
          }
        }
      })
      // #endif
      return false
    },
    /**
     * 设置购物车tabbar的角标
     * 该方法只能在tabbar页面中调用, 其他页面调用会报错
     */
    setCartTabBadge(totalNum = -1) {
      const self = this
      if (totalNum === -1) {
        self.$u.api.getCartList().then((res) => {
          let totalNum = 0
          res.data.forEach((gorup) => {
            gorup.items.forEach((item) => {
              //totalNum += 1;
              totalNum += item.count
            })
          })
          console.debug('uTabbar set cartTotalNum=' + totalNum)
          if (self.list[cartTabbarIndex]) {
            self.list[cartTabbarIndex].count = totalNum
          }
        })
        return
      }
      console.debug('uTabbar set cartTotalNum=' + totalNum)
      if (self.list[cartTabbarIndex]) {
        self.list[cartTabbarIndex].count = totalNum
      }
    }
  }
}
</script>
